package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;

import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.model.ModelUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

public class RegisterTask extends BaseDataAsyncTask<Map<String,String>, Void, String> {
	private final static String TAG = "RegisterTask";
	public RegisterTask(
			Context context,
			int what,
			DataAsyncCallback callback) {
		super(context, what, callback);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected String doInBackground(Map<String,String>... params) {
		if(null==params){
			return null;
		}
		Map<String,String> username = params[0];
		String result = null;
		Map<String,Object> returnjson = null;
        try {            
        	List<NameValuePair> values = new ArrayList<NameValuePair>();
        	values.add(new BasicNameValuePair("username", username.get("username")));
        	values.add(new BasicNameValuePair("password", username.get("password")));
        	values.add(new BasicNameValuePair("password2", username.get("password2")));
        	values.add(new BasicNameValuePair("authcode", username.get("authcode")));
        	values.add(new BasicNameValuePair("email", username.get("email")));
        	values.add(new BasicNameValuePair("status", "2"));
        	
        	//JSONObject jsonObject = HttpUtils.getHttpJSONObjectResult(getHttpClient(), Constant.URL_REGISTER_PATH, values, HttpUtils.POST);
        	HttpResponse httpResponse = HttpUtils.doHttpExecute(getHttpClient(), Constant.URL_REGISTER_PATH, values, null, HttpUtils.POST, "GBK");
        	
        	if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        		String responseStr = EntityUtils.toString(httpResponse.getEntity());
        		JSONObject json = new JSONObject(responseStr);
        		if (json != null) {
            		returnjson = ModelUtils.json2Map(json);
            		result = (String) returnjson.get("code");
    			}
        	}
        	
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        } finally {
            closeHttpClient();
        }
        
        return result;
	}
}
